<?php

class Tools {

	public function mydump($data){
    	// 定义样式
    	$str='<pre style="display: block;padding: 9.5px;margin: 44px 0 0 0;font-size: 13px;line-height: 1.42857;color: #333;word-break: break-all;word-wrap: break-word;background-color: #F5F5F5;border: 1px solid #CCC;border-radius: 4px;">';
	    // 如果是boolean或者null直接显示文字；否则print
	    if (is_bool($data)) {

	        $show_data=$data ? 'true' : 'false';

	    }elseif (is_null($data)) {

	        $show_data='null';

	    }else{

	        $show_data=print_r($data,true);

	    }
	    $str.=$show_data;
	    $str.='</pre>';
	    echo $str;
	}
	public function file_format($str){
	    // 取文件后缀名
	    $str=strtolower(pathinfo($str, PATHINFO_EXTENSION));
	    // 图片格式
	    $image=array('webp','jpg','png','ico','bmp','gif','tif','pcx','tga','bmp','pxc','tiff','jpeg','exif','fpx','svg','psd','cdr','pcd','dxf','ufo','eps','ai','hdri');
	    // 视频格式
	    $video=array('mp4','avi','3gp','rmvb','gif','wmv','mkv','mpg','vob','mov','flv','swf','mp3','ape','wma','aac','mmf','amr','m4a','m4r','ogg','wav','wavpack');
	    // 压缩格式
	    $zip=array('rar','zip','tar','cab','uue','jar','iso','z','7-zip','ace','lzh','arj','gzip','bz2','tz');
	    // 文档格式
	    $text=array('exe','doc','ppt','xls','wps','txt','lrc','wfs','torrent','html','htm','java','js','css','less','php','pdf','pps','host','box','docx','word','perfect','dot','dsf','efe','ini','json','lnk','log','msi','ost','pcs','tmp','xlsb');
	    // 匹配不同的结果
	    switch ($str) {
	        case in_array($str, $image):
	            return 'image';
	            break;
	        case in_array($str, $video):
	            return 'video';
	            break;
	        case in_array($str, $zip):
	            return 'zip';
	            break;
	        case in_array($str, $text):
	            return 'text';
	            break;
	        default:
	            return 'image';
	            break;
    		}
    }
	/**
	 * 字符串截取，支持中文和其他编码
	 * @param string $str 需要转换的字符串
	 * @param string $start 开始位置
	 * @param string $length 截取长度
	 * @param string $suffix 截断显示字符
	 * @param string $charset 编码格式
	 * @return string
	 */
	public function re_substr($str, $start=0, $length, $suffix=true, $charset="utf-8") {
	    if(function_exists("mb_substr"))
	        $slice = mb_substr($str, $start, $length, $charset);
	    elseif(function_exists('iconv_substr')) {
	        $slice = iconv_substr($str,$start,$length,$charset);
	    }else{
	        $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
	        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
	        $re['gbk']  = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
	        $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
	        preg_match_all($re[$charset], $str, $match);
	        $slice = join("",array_slice($match[0], $start, $length));
	    }
	    $omit=mb_strlen($str) >=$length ? '...' : '';
	    return $suffix ? $slice.$omit : $slice;
	}
	/**
	 * 获取一定范围内的随机数字
	 * 跟rand()函数的区别是 位数不足补零 例如
	 * rand(1,9999)可能会得到 465
	 * rand_number(1,9999)可能会得到 0465  保证是4位的
	 * @param integer $min 最小值
	 * @param integer $max 最大值
	 * @return string
	 */
	public function rand_number ($min=1, $max=9999) {
	    return sprintf("%0".strlen($max)."d", mt_rand($min,$max));
	}
		/**
	 * 生成一定数量的随机数，并且不重复
	 * @param integer $number 数量
	 * @param string $len 长度
	 * @param string $type 字串类型
	 * 0 字母 1 数字 其它 混合
	 * @return string
	 */
	public function build_count_rand ($number,$length=4,$mode=1) {
	    if($mode==1 && $length<strlen($number) ) {
	        //不足以生成一定数量的不重复数字
	        return false;
	    }
	    $rand   =  array();
	    for($i=0; $i<$number; $i++) {
	        $rand[] = rand_string($length,$mode);
	    }
	    $unqiue = array_unique($rand);
	    if(count($unqiue)==count($rand)) {
	        return $rand;
	    }
	    $count   = count($rand)-count($unqiue);
	    for($i=0; $i<$count*3; $i++) {
	        $rand[] = rand_string($length,$mode);
	    }
	    $rand = array_slice(array_unique ($rand),0,$number);
	    return $rand;
	}

	/**
	 * 生成不重复的随机数
	 * @param  int $start  需要生成的数字开始范围
	 * @param  int $end 结束范围
	 * @param  int $length 需要生成的随机数个数
	 * @return array       生成的随机数
	 */
	public function get_rand_number($start=1,$end=10,$length=4){
	    $connt=0;
	    $temp=array();
	    while($connt<$length){
	        $temp[]=rand($start,$end);
	        $data=array_unique($temp);
	        $connt=count($data);
	    }
	    sort($data);
	    return $data;
	}
		/*
	* 计算星座的函数 string get_zodiac_sign(string month, string day)
	* 输入：月份，日期
	* 输出：星座名称或者错误信息
	*/

	public function get_zodiac_sign($month, $day)
	{
	    // 检查参数有效性
	    if ($month < 1 || $month > 12 || $day < 1 || $day > 31)
	        return (false);
	        // 星座名称以及开始日期
	        $signs = array(
	        array( "20" => "水瓶座"),
	        array( "19" => "双鱼座"),
	        array( "21" => "白羊座"),
	        array( "20" => "金牛座"),
	        array( "21" => "双子座"),
	        array( "22" => "巨蟹座"),
	        array( "23" => "狮子座"),
	        array( "23" => "处女座"),
	        array( "23" => "天秤座"),
	        array( "24" => "天蝎座"),
	        array( "22" => "射手座"),
	        array( "22" => "摩羯座")
	    );
	    list($sign_start, $sign_name) = each($signs[(int)$month-1]);
	    if ($day < $sign_start)
	    list($sign_start, $sign_name) = each($signs[($month -2 < 0) ? $month = 11: $month -= 2]);
	    return $sign_name;
	}
	/**
	 * 将路径转换加密
	 * @param  string $file_path 路径
	 * @return string            转换后的路径
	 */
	public function path_encode($file_path){
	    return rawurlencode(base64_encode($file_path));
	}

	/**
	 * 将路径解密
	 * @param  string $file_path 加密后的字符串
	 * @return string            解密后的路径
	 */
	public function path_decode($file_path){
	    return base64_decode(rawurldecode($file_path));
	}
		/**
	 * 传入时间戳,计算距离现在的时间
	 * @param  number $time 时间戳
	 * @return string     返回多少以前
	 */
	public function word_time($time) {
	    $time = (int) substr($time, 0, 10);
	    $int = time() - $time;
	    $str = '';
	    if ($int <= 2){
	        $str = sprintf('刚刚', $int);
	    }elseif ($int < 60){
	        $str = sprintf('%d秒前', $int);
	    }elseif ($int < 3600){
	        $str = sprintf('%d分钟前', floor($int / 60));
	    }elseif ($int < 86400){
	        $str = sprintf('%d小时前', floor($int / 3600));
	    }elseif ($int < 1728000){
	        $str = sprintf('%d天前', floor($int / 86400));
	    }else{
	        $str = date('Y-m-d H:i:s', $time);
	    }
	    return $str;
	}
	/**
	 * 检测是否是手机访问
	 */
	public function is_mobile(){
	    $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
	    $useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';
	    function _is_mobile($substrs,$text){
	        foreach($substrs as $substr)
	            if(false!==strpos($text,$substr)){
	                return true;
	            }
	            return false;
	    }
	    $mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');
	    $mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');

	    $found_mobile=_is_mobile($mobile_os_list,$useragent_commentsblock) ||
	              _is_mobile($mobile_token_list,$useragent);
	    if ($found_mobile){
	        return true;
	    }else{
	        return false;
	    }
	}
	/**
	 * 获取当前访问的设备类型
	 * @return integer 1：其他  2：iOS  3：Android
	 */
	function get_device_type(){
	    //全部变成小写字母
	    $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
	    $type = 1;
	    //分别进行判断
	    if(strpos($agent, 'iphone')!==false || strpos($agent, 'ipad')!==false){
	        $type = 2;
	    } 
	    if(strpos($agent, 'android')!==false){
	        $type = 3;
	    }
	    return $type;
	}


}